package com.rlz.framemm.ui.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.rlz.base.base.BaseHolder;
import com.rlz.base.tools.adapter.BaseAdapter;
import com.rlz.framemm.BR;
import com.rlz.framemm.R;
import com.rlz.framemm.bean.ArticleBean;
import com.rlz.framemm.config.Image;
import com.rlz.framemm.databinding.ItemListBinding;
import com.rlz.image.imageloader.ImageLoader;

import androidx.databinding.BindingAdapter;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;


/**
 * Created by RLZ
 * on 2021/4/8
 */
public class ArticleAdapter extends BaseAdapter<ArticleBean.ListBean> {

    @Override
    public RecyclerView.ViewHolder getViewHolder(ViewGroup viewGroup, int viewType) {
        ItemListBinding binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.item_list, viewGroup, false);
        ArticleBean.ListBean listBean = mData.get(viewType);
        binding.setVariable(BR.bean, listBean);
        return new BaseHolder(binding);
    }

    @Override
    public void onBindHolder(RecyclerView.ViewHolder viewHolder, int i) {
        ItemListBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
        if (binding == null) return;
        ArticleBean.ListBean itemData = mData.get(i);
        binding.setVariable(BR.bean, itemData);
        binding.executePendingBindings();
    }


    @BindingAdapter("ImageUrl")
    public static void loadImageUrl(ImageView imageView, String url) {
        //Glide.with(imageView.getContext()).load(url).into(imageView);
        Image image = new Image();
        image.imageView = imageView;
        image.url = url;
        image.placeholder = R.mipmap.ic_launcher;
        image.errorId = R.mipmap.ic_launcher;
        ImageLoader.get().loadImage(imageView.getContext(), image);
    }
}
